﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlphaBeatSoundEngine
{
    public enum ToneMeasureLength
    {
        QuarterTime,
        HalfTime,
        Time,
        DoubleTime,
        QuadrupleTime,
        EightTime
    }

    public static class ToneLengthExtensions
    {
        public static String ToFractionnedString(this ToneMeasureLength tonelength)
        {
            switch (tonelength)
            {
                case ToneMeasureLength.QuarterTime: return "¼";
                case ToneMeasureLength.HalfTime: return "½";
                case ToneMeasureLength.Time: return "1";
                case ToneMeasureLength.DoubleTime: return "2";
                case ToneMeasureLength.QuadrupleTime: return "4";
                case ToneMeasureLength.EightTime: return "8";
                default: return "";
            }
        }
        
        public static int TimeInMillisecondForTempo(this ToneMeasureLength tonelength, int tempo)
        {
            float timeInSecondForOneMeasure = 60.0F / tempo * 4;
            float timeInSecondForGivenLength = timeInSecondForOneMeasure * tonelength.FractionOfMeasure();
            float timeInMillisecondForGivenLength = 1000 * timeInSecondForGivenLength;
            return (int)timeInMillisecondForGivenLength;
        }

        public static float FractionOfMeasure(this ToneMeasureLength toneLength)
        {
            switch (toneLength)
            {
                case ToneMeasureLength.DoubleTime: return 2.0F;
                case ToneMeasureLength.Time: return 1.0F;
                case ToneMeasureLength.HalfTime: return 0.5F;
                case ToneMeasureLength.QuadrupleTime: return 4.0F;
                case ToneMeasureLength.QuarterTime: return 0.25F;

                default: return 1.0F;
            }
        }
    }
}
